package com.example.milktea.dao.Impl;

import com.example.milktea.dao.MaterialDao;
import com.example.milktea.entity.Material;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;


@Repository
public class MaterialDaoImpl implements MaterialDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Material material) {
        //添加新材料，同时添加一条库存store数据
        String sql = "insert into material (material_id,material_name,supplement_phone,supplement_price) values (?,?,?,?) ";
        this.jdbcTemplate.update(
                sql,
                material.getMaterial_id(),
                material.getMaterial_name(),
                material.getSupplement_phone(),
                material.getSupplement_price());
        String sql1 = "insert into store (material_id,store_number) values (?,?)";
        return this.jdbcTemplate.update(
                sql1,
                material.getStore().getMaterial_id(),
                material.getStore().getStoreNumber()
        );
    }

    @Override
    public int updateSupplementPhone(Material material) {
        String sql = "update material set supplement_phone = ? where material_id = ?";
        return this.jdbcTemplate.update(
                sql,
                material.getSupplement_phone(),
                material.getMaterial_id()
        );
    }

    @Override
    public int updateSupplementPrice(Material material) {
        String sql = "update material set supplement_price = ? where material_id = ?";
        return this.jdbcTemplate.update(
                sql,
                material.getSupplement_price(),
                material.getMaterial_id()
                );
    }
}
